package com.moriarty.socketim.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.moriarty.socketim.IMApplication;
import com.moriarty.socketim.R;
import com.moriarty.socketim.core.OnMessageListener;
import com.moriarty.socketim.core.QQConnection;
import com.moriarty.socketim.entity.QQMessage;
import com.moriarty.socketim.entity.QQMessageType;
import com.moriarty.socketim.service.MessageService;
import com.moriarty.socketim.util.ThreadUtil;

/**
 * Created by Moriarty on 16/11/16.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtUsername;
    private EditText mEtPwd;
    private Button mBtnLogin;

    private final String ip = "192.168.0.102";
    private final int port = 5225;
    private QQConnection mQqConnection;
    private String username;
    private String json_buddylist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        mEtUsername = (EditText) findViewById(R.id.et_username);
        mEtPwd = (EditText) findViewById(R.id.et_pwd);
        mBtnLogin = (Button) findViewById(R.id.btn_login);

        mBtnLogin.setOnClickListener(this);
        //一旦创建 就要开启连接 并开始监听来自服务器的消息
        ThreadUtil.runOnSubThread(new Runnable() {
            @Override
            public void run() {
                mQqConnection = new QQConnection(ip, port);
                mQqConnection.addOnMessageListener(listener);
                mQqConnection.connect();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                //获取输入的信息 然后传送给服务器
                username = mEtUsername.getText().toString().trim();
                String pwd = mEtPwd.getText().toString().trim();
                //封装到qqMessage里面
                final QQMessage qqMessage = new QQMessage();
                qqMessage.content = username + "#" + pwd;
                qqMessage.type = QQMessageType.MSG_TYPE_LOGIN;
                //传送给服务器
                ThreadUtil.runOnSubThread(new Runnable() {
                    @Override
                    public void run() {
                        //将msg看成表单对象（只是没有用map 用了对象）
                        mQqConnection.sendMessage(qqMessage);
                    }
                });
                break;
        }
    }

    //接口类型的变量的自动补全必须从头开始写 不然出不来
    OnMessageListener listener = new OnMessageListener() {
        @Override
        public void onGetMessage(final QQMessage msg) {

            //因为要处理界面 所以登录代码写在主线程
            ThreadUtil.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    if (QQMessageType.MSG_TYPE_BUDDY_LIST.equals(msg.type)) {
                        //获取好友列表
                        json_buddylist = msg.content;

                        Log.e("buddylist from server", json_buddylist);
                        //将返回的账号名和好友列表存入application 便于其他页面使用
                        IMApplication imApplication = (IMApplication) getApplication();
                        imApplication.setUsername(username);
                        imApplication.setQqConnection(mQqConnection);
                        //提示登录成功
                        Toast.makeText(getApplicationContext(), "login success!", Toast.LENGTH_SHORT).show();
                        //开启服务 维持长连接
                        Intent serviceIntent = new Intent(getBaseContext(), MessageService.class);
                        startService(serviceIntent);
                        //跳转
                        Intent intent = new Intent(getBaseContext(), BuddyListActivity.class);
                        intent.putExtra("buddylistjson", json_buddylist);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(getApplicationContext(), "wrong password!", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    };

    //界面销毁的时候 将listener拿掉
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mQqConnection.removeOnMessageListener(listener);
    }
}
